package com.li.stack;

//栈实现综合计算器
public class Calculateor {
    public static void main(String[] args) {
        String ex = "3+2*6-2";
        //String ex = "2*6";

        Arraystack2 numstack = new Arraystack2(10);
        Arraystack2 operstack = new Arraystack2(10);
        int index = 0;
        int num1 = 0;
        int num2 = 2;
        int oper = 0;
        int res = 0;
        char ch = ' ';
        String keepnum = "";
        while (true) {
            ch = ex.substring(index, index + 1).charAt(0);//依次得到每个字符
            //如果是运算符
            if (operstack.isoper(ch)) {
                //运算符栈是不是为空
                if (!operstack.isempty()) {
                    if (operstack.pre(ch) <= operstack.pre(operstack.peak())) {
                        num1 = numstack.pop();
                        num2 = numstack.pop();
                        oper = operstack.pop();
                        res = numstack.cal(num1, num2, oper);
                        numstack.push(res);
                        operstack.push(ch);
                    } else {
                        operstack.push(ch);
                    }
                } else {
                    operstack.push(ch);
                }
            } else {
                //numstack.push(ch-48);//数字注解注入
                //当实现是多为数，可能是多位数
                //因此，需要定义字符串 ，用于拼接
                keepnum += ch;
                if (index == ex.length() - 1) {
                    numstack.push(Integer.parseInt(keepnum));
                } else {
                    if (operstack.isoper(ex.substring(index + 1, index + 2).charAt(0))) {
                        numstack.push(Integer.parseInt(keepnum));
                        keepnum = "";
                    }
                }
            }
            index++;
            if (index >= ex.length()) {
                break;
            }
        }
        while (true) {
            if (operstack.isempty()) {
                break;
            }
            num1 = numstack.pop();
            num2 = numstack.pop();
            oper = operstack.pop();
            res = numstack.cal(num1, num2, oper);
            numstack.push(res);
        }
        System.out.printf("表达式%s是=%d", ex, numstack.pop());
    }

}

class Arraystack2 {
    private int maxSize;
    private int[] stack;
    private int top = -1;

    public Arraystack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    public int peak() {
        return stack[top];
    }

    public boolean isfull() {
        return top == maxSize - 1;
    }

    public boolean isempty() {
        return top == -1;
    }

    public void push(int value) {
        if (isfull()) {
            System.out.println("堆满");
            return;
        } else {
            top++;
            stack[top] = value;
        }

    }

    //出栈
    public int pop() {
        if (isempty()) {
            throw new RuntimeException("没有数据");
        }
        int value = stack[top];
        top--;
        return value;
    }

    public void list() {
        if (isempty()) {
            throw new RuntimeException("没有数据");
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]%d", i, stack[i]);
        }
    }

    public int pre(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }

    //判断是不是运算符·
    public boolean isoper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    //运算·方法
    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num2 * num1;
                break;
            case '/':
                res = num2 / num1;
                break;
        }
        return res;
    }
}
